$(function () {
    //1.抽取变量
    var customer_form = $("#customer_form");
    var customer_datagrid = $("#customer_datagrid");
    var customer_dialog = $("#customer_dialog");

    $("#customer_form :input").prop("disabled",true);

    //用户数据表格
    customer_datagrid.datagrid({
        url: "/customer/list",
        fit: true,
        fitColumns: true,
        striped: true,
        toolbar: '#customer_toolbar',
        pagination: true,
        singleSelect: true,
        rownumbers: true,
        columns: [[
            {field: 'inputDate', title: '创建日期', width: 100},

            {
                field: 'inputUser', title: '创建人', width: 60, formatter: function (value, row, index) {
                return value ? value.realName : '';
            }
            },
            {
                field: 'chargeUser', title: '负责人', width: 60, formatter: function (value, row, index) {
                return value ? value.realName : '';
            }
            },
            {field: 'name', title: '客户姓名', width: 70},
            {field: 'age', title: '年龄', width: 35},
            {
                field: 'gender', title: '性别', width: 35, formatter: function (value, row, index) {
                if (value) {
                    return '男';
                }
                else {
                    return '女';
                }
            }
            },
            {field:'job',title:'职业',width:100,formatter:function (value, row, index) {
                return value ? value.name : '';
            }},
            {field:'salaryLevel',title:'薪资水平',width:100,formatter:function (value, row, index) {
                return value ? value.name : '';
            }},
            {field:'tel',title:'电话',width:100},
            {field:'email',title:'邮箱',width:100},
            {field:'address',title:'客户地址',width:100},
            {field:'source',title:'客户来源',width:100,formatter:function (value, row, index) {
                return value ? value.name : '';
            }},
            {field: 'qq', title: 'QQ', width: 80},
            {field: 'wechat', title: '微信', width: 100},
            {field: 'idNo', title: '身份证号', width: 100},
            {
                field: 'status', title: '状态', width: 100, formatter: function (value, row, index) {
                if (value == 1) {
                    return '潜在客户';
                }
                else if (value == 2) {
                    return '正式客户';
                } else if (value == 0){
                    return '资源池'
                }else{
                    return '流失客户'
                }
            }
            }

        ]],
    })

    //查看详情的弹出框
    customer_dialog.dialog({
        width: 350,
        height: 450,
        buttons: '#customer_btns',
        closed: true,
        shadow:true
    })


    var cmdObj = {

        view: function () {
            //获取选中数据
            var row = customer_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选中一条数据！', 'info');
                return;
            }
            customer_form.form("clear");


            if (row.gender) {
                row["gender"] = "男"
            }else {
                row["gender"] = "女"
            }
            if (row.job) {
                row["job.name"] = row.job.name;
            }
            if (row.source) {
                row["source.name"] = row.source.name;
            }
            if (row.salaryLevel) {
                row["salaryLevel.name"] = row.salaryLevel.name;
            }
            customer_form.form("load", row);

            customer_dialog.dialog("setTitle", "详细信息");

            customer_dialog.dialog("center");
            customer_dialog.dialog("open");
        },

        cancel: function () {
            customer_dialog.dialog("close");
        }
    }
    //统一执行方法
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    })
})

function customer_search() {


    var idNo = $("input[name=idNo]").val();
    var chargeUser_name = $("input[name=chargeUser_name]").val();
    var tel = $("input[name=tel]").val();
    var beginDate = $("input[name=beginDate]").val();
    var endDate = $("input[name=endDate]").val();
    var keyword = $("input[name=keyword]").val();

    $("#customer_datagrid").datagrid("load",{
        keyword:keyword,
        chargeUser_name:chargeUser_name,
        tel:tel,
        idNo:idNo,
        beginDate:beginDate,
        endDate:endDate
    })
}